/**
 * Copyright (c) 2018, 西安星沙网络科技-版权所有
 *
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.sstech.member.core.util.pagination;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;


/**
 * 分页对象
 * @author chenx
 * @date 2018年3月22日 下午2:02:02
 * @version 1.0 
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class PageInfo<T> implements Serializable {

	/**
	 * 我是20号晚上10点多充的币，当时你们系统在升级给
	 */
	private static final long serialVersionUID = -414333585287819433L;

	/**
	 * 当前页
	 */
    private long pageNumber;
    /**
     * 每页的数量
     */
    private long pageSize;
    /**
     * 总记录数
     */
    private long totalRow;
    /**
     * 总页数
     */
    private long totalPage;
    /**
     * 结果集
     */
    private List<T> list;
    /**
     * 是否为第一页
     */
    private boolean isFirstPage = false;
    /*
     * 是否为最后一页
     */
    private boolean isLastPage = false;
    
    public PageInfo() {
    }
    
    /**
     * 包装Page对象
     *
     * @param list
     */
	public PageInfo(List<T> list) {
        if (list instanceof Collection) {
            this.pageNumber = 1;
            this.pageSize = list.size();

            this.totalPage = 1;
            this.list = list;
            this.totalRow = list.size();
        }
        if (list instanceof Collection) {
            //判断页面边界
            judgePageBoudary();
        }
    }
	
	/**
     * 判定页面边界
     */
    private void judgePageBoudary() {
        isFirstPage = pageNumber == 1;
        isLastPage = pageNumber == totalPage;
    }

	public long getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(long pageNumber) {
		this.pageNumber = pageNumber;
	}

	public long getPageSize() {
		return pageSize;
	}

	public void setPageSize(long pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(long totalRow) {
		this.totalRow = totalRow;
	}

	public long getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(long totalPage) {
		this.totalPage = totalPage;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public boolean isFirstPage() {
		return isFirstPage;
	}

	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public boolean isLastPage() {
		return isLastPage;
	}

	public void setLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}
    
	@Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("PageInfo{");
        sb.append("pageNumber=").append(pageNumber);
        sb.append(", pageSize=").append(pageSize);
        sb.append(", totalRow=").append(totalRow);
        sb.append(", totalPage=").append(totalPage);
        sb.append(", list=").append(list);
        sb.append(", isFirstPage=").append(isFirstPage);
        sb.append(", isLastPage=").append(isLastPage);
        sb.append(", navigatepageNums=");
        sb.append('}');
        return sb.toString();
    }
	
	public void setTotalPageByTotalRowAndSize(){
		if(this.getPageSize() == -1){
			this.setTotalPage(1);
		}
		else{
			if(this.getTotalRow()%this.getPageSize() == 0){
				this.setTotalPage((this.getTotalRow()-this.getTotalRow()%this.getPageSize())/this.getPageSize());
			}
			else{
				this.setTotalPage((this.getTotalRow()-this.getTotalRow()%this.getPageSize())/this.getPageSize()+1);
			}
		}
		
	}
}
